﻿using Smart.Model;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class CameraSetting
{
    public Vector3 pos;
    public Vector3 rotation;
}

public class CameraBinder : MonoBehaviour
{
    [SerializeField]
    protected CameraSetting[] settings = new CameraSetting[0];

    protected int configId = 0;
    protected Camera ca;

    void Awake()
    {
        instance = this;
        ca = GetComponent<Camera>();
        Log.Info($"[场景相机]:启动成功...");
    }

    public void SetAngleX(float deltax)
    {
        var src = transform.localEulerAngles;
        var dst = src;
        if (dst.x < 0)
            dst.x += 360;

        if (dst.x >= 270.0f && dst.x <= 360.0f)
        {
            if (deltax > 0)
            {
                if (dst.x + deltax <= 450.0f)
                    dst.x += deltax;
            }
            else
            {
                if (dst.x + deltax >= 270.0f)
                    dst.x += deltax;
            }
        }
        else if (dst.x >= 0 && dst.x <= 90)
        {
            if (deltax > 0)
            {
                if (dst.x + deltax <= 90)
                    dst.x += deltax;
            }
            else
            {
                if (dst.x + deltax >= -90.0f)
                    dst.x += deltax;
            }
        }

        transform.localEulerAngles = dst;
    }

    public void SetConfig(int id)
    {
        if (id >= 0 && id < settings.Length)
        {
            configId = id;
            var setting = settings[configId];
            if (null != setting)
            {
                transform.localPosition = setting.pos;
                transform.localRotation = Quaternion.Euler(setting.rotation);
            }
        }
    }

    static CameraBinder instance;
    public static CameraBinder Instance
    {
        get
        {
            return instance;
        }
    }

    public void SetParent(Transform ts)
    {
        if (null != ts)
            transform.SetParent(ts);
    }

    private void OnDestroy()
    {
        if (instance == this)
            instance = null;
    }
}